(function () {
    'use strict';

    angular.module('app')
        .controller('SysDictFormCtrl', SysDictFormCtrl);

    SysDictFormCtrl.$inject = ['$rootScope', '$scope', '$state', 'toaster', 'SysDictService'];
    function SysDictFormCtrl($rootScope, $scope, $state, toaster, sysDictService) {


        var id = $state.params.id;
        var parentId = $state.params.parentId;

        if (!!id) { //编辑
            $scope.title = '编辑字典';
            $rootScope.loading = true;
            sysDictService.getById(id)
                .then(function (data) {
                    $scope.sysDict = data;
                })
                .finally(function () {
                    $rootScope.loading = false;
                });
        } else if (!id && !parentId) { //新建
            $scope.title = '新建字典';
            $scope.menu = {
                id: '',
                parentId: '',
                sort: 0
            };
        } else if (!id && !!parentId) { //添加子菜单
            $scope.title = '添加子字典';
            $scope.sysDict = {
                id: '',
                parentId: parentId,
                sort: 0
            };
        }
        $scope.saveData = function () {
            $rootScope.loading = true;
            sysDictService.saveData($scope.sysDict)
                .then(function () {
                    toaster.pop('success', '', '保存成功');
                })
                .finally(function () {
                    $rootScope.loading = false;
                })
            ;
        }

    }
})
();